package com.ruoyi.project.payment.order.controller;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.IpUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.payment.order.domain.Order;
import com.ruoyi.project.payment.order.service.IOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 支付订单Controller
 *
 * @author shixiangwen
 * @date 2019-12-04
 */
@Controller
@RequestMapping("/payment/order")
public class OrderController extends BaseController {
    private String prefix = "payment/order";

    @Autowired
    private IOrderService orderService;

    @RequiresPermissions("payment:order:view")
    @GetMapping()
    public String order() {
        return prefix + "/order";
    }

    /**
     * 查询支付订单列表
     */
    @RequiresPermissions("payment:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Order order) {
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 导出支付订单列表
     */
    @RequiresPermissions("payment:order:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Order order) {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 新增支付订单
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存支付订单
     */
    @RequiresPermissions("payment:order:add")
    @Log(title = "支付订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Order order) {
        return toAjax(orderService.insertOrder(order));
    }

    /**
     * 修改支付订单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Order order = orderService.selectOrderById(id);
        mmap.put("order", order);
        return prefix + "/edit";
    }

    /**
     * 修改保存支付订单
     */
    @RequiresPermissions("payment:order:edit")
    @Log(title = "支付订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Order order) {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除支付订单
     */
    @RequiresPermissions("payment:order:remove")
    @Log(title = "支付订单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(orderService.deleteOrderByIds(ids));
    }

    /**
     * 补单
     *
     * @param id
     * @return
     */
    @RequiresPermissions("payment:order:edit")
    @Log(title = "支付订单", businessType = BusinessType.GRANT)
    @PostMapping("/reNotify")
    @ResponseBody
    public AjaxResult reNotify(Long id) {
        return toAjax(orderService.reNotify(id));
    }

    @RequiresPermissions("payment:order:edit")
    @Log(title = "订单退款", businessType = BusinessType.GRANT)
    @PostMapping("/refundNotify")
    @ResponseBody
    public AjaxResult refundNotify(HttpServletRequest request, Long id, String refundRemark) {
        String ip = IpUtils.getIpAddr(request);
        String remark = "退款时间[" + DateUtils.dateTimeNow() + "], 操作人[" + ShiroUtils.getLoginName() + "], 操作IP[" + ip + "], 退款说明[" + refundRemark + "]";
        return toAjax(orderService.refundNotify(id, remark));
    }
}
